//
//  ReportCardDetailPageVC.m
//  jzyParents
//
//  Created by 任辰东 on 2022/9/19.
//

#import "ReportCardDetailPageVC.h"
#import "ReportCardDetailVC.h"
#import "ReportCardKemuModel.h"
#import "ReportApis.h"
#import "RCDPageViewController.h"
#import "ReportCardDetailVC.h"
#import "UIImage+color.h"
#import "UIButton+BackgroundColor.h"

@interface ReportCardDetailPageVC ()
@property (nonatomic, strong) RCDPageViewController *pageController;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *markView;
@property (nonatomic, strong) UIButton *allBtn;
@property (nonatomic, strong) UIButton *cuoBtn;
@property (nonatomic, strong) ReportCardModel *model;
@property (nonatomic, strong) ReportCardKemuModel *kemuModel;
@property (nonatomic, strong) NSArray <Xiaoti_score_tongjiItem *>*menuArray;

@property (nonatomic, assign) BOOL isAll;
@end

@implementation ReportCardDetailPageVC

-(instancetype)initWithReportCardModel:(ReportCardModel *)model withReportCardKemuModel:(ReportCardKemuModel *)kemuModel
{
    self = [super init];
    if (self) {
        _kemuModel = kemuModel;
        _model = model;
    }
    return self;
}

- (void)getMenuData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = [GVUserDefaults standardUserDefaults].student_id;
    params[@"main_id"] = self.model.main_id;
    params[@"kaoshi_type"] = self.model.is_liankao;
    params[@"kemu_id"] = self.kemuModel.kemu_id;
    params[@"shiti_type"] = self.isAll ? @"1":@"2";
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis ctb_Xq_ShitiListRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        NSArray *data = [Xiaoti_score_tongjiItem mj_objectArrayWithKeyValuesArray:responseData];
        self.menuArray = [NSArray arrayWithArray:data];
        [self reloadData];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

- (void)viewDidLoad {

    self.showOnNavigationBar = NO;
    self.pageAnimatable = YES;
    self.progressColor = UIColorFromHex(kColorMain);
    self.titleColorNormal = UIColorFromHex(0x999999);
    self.titleColorSelected = UIColorFromHex(kColorMain);
    self.menuView.backgroundColor = [UIColor whiteColor];
//    self.menuView.height = 50;
    self.postNotification = YES;
    self.bounces = YES;
    self.menuViewStyle = WMMenuViewStyleDefault;
    self.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    self.titleSizeSelected = 16;
    self.titleSizeNormal = 15;
    self.automaticallyCalculatesItemWidths = YES;
//    self.menuItemWidth = 20;
    self.itemMargin = 15.0;
    [super viewDidLoad];
    
    [self configView];
    self.isAll = YES;
    self.allBtn.selected = YES;
    self.cuoBtn.selected = NO;
    
    [self getMenuData];
    [self setupViews];
    [self setupLayout];
}

-(void)allBtnAction
{
   if (!self.isAll) {
       self.isAll = YES;
       self.allBtn.selected = YES;
       self.cuoBtn.selected = NO;
       [self getMenuData];
   }

}

-(void)cuoBtnAction
{
   if (self.isAll) {
       self.isAll = NO;
       self.allBtn.selected = NO;
       self.cuoBtn.selected = YES;
       [self getMenuData];
   }

}

-(void)setupViews
{
    [self.view addSubview:self.containerView];
    [self.containerView addSubview:self.markView];
    [self.containerView addSubview:self.allBtn];
    [self.containerView addSubview:self.cuoBtn];
    
    [self.allBtn addTarget:self action:@selector(allBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.cuoBtn addTarget:self action:@selector(cuoBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setupLayout
{
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).offset(40);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
    [self.markView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.top.equalTo(self.containerView);
        make.height.mas_equalTo(10);
    }];
    
    [self.allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.containerView).offset(20);
        make.left.equalTo(self.containerView).offset(15);
        make.bottom.equalTo(self.containerView).offset(-10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(65);
    }];
    
    [self.cuoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.containerView).offset(20);
        make.left.equalTo(self.allBtn.mas_right).offset(15);
        make.bottom.equalTo(self.containerView).offset(-10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(65);
    }];
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController
{
    return self.menuArray.count;
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index
{
    
    Xiaoti_score_tongjiItem *model = self.menuArray[index];
    ReportCardDetailVC *showVC = [[ReportCardDetailVC alloc]initWithReportCardModel:self.model withXiaoti_score_tongjiItem:model];
    return showVC;
}


- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index
{
    Xiaoti_score_tongjiItem *model = self.menuArray[index];
    return model.ti_name_show;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    
    CGFloat height = 40;
    CGFloat originY = 0;
    return CGRectMake(0, originY, self.view.frame.size.width, height);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY + 50, self.view.frame.size.width, self.view.frame.size.height - originY - 50);
}

-(void)configView
{
    self.navigationItem.title = @"试题详情";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kNavitionBarHeight)] forBarMetrics:UIBarMetricsDefault];
    
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:UIColorFromHex(kColorLine) size:CGSizeMake(kScreenWidth, kOnePixel)]];
    
    if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc]init];
            [navigationBarAppearance configureWithOpaqueBackground];
            [navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kNavitionBarHeight)]];
            navigationBarAppearance.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromHex(kColorWhite),NSFontAttributeName:UIFontSize(21)};
            [navigationBarAppearance setShadowImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kOnePixel)]];
            self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
            self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
        }
    
    if (self.navigationController.viewControllers.count > 1) {
        [self setNavLeftItemWithImage:UIImageWithName(@"back_white") highlightImg:nil];
    }
    if (self.tabBarController == nil && kKeyWindow.rootViewController != self.navigationController) {
        [self setNavLeftItemWithImage:UIImageWithName(@"back_white") highlightImg:nil];
    }
    
    //防止自定义返回按钮后返回手势失效
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

-(UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _containerView;
}

-(UIView *)markView
{
    if (!_markView) {
        _markView = [UIView new];
        _markView.backgroundColor = UIColorFromHex(kColorBG);
    }
    return _markView;
}

-(UIButton *)allBtn
{
    if (!_allBtn) {
        _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_allBtn setBackgroundColor:UIColorFromHex(kColorWhite) forState:UIControlStateNormal];
        [_allBtn setBackgroundColor:UIColorFromHex(kColorMain) forState:UIControlStateSelected];
        [_allBtn setTitle:@"全部试题" forState:UIControlStateNormal];
        [_allBtn setTitleColor:UIColorFromHex(kColorWhite) forState:UIControlStateSelected];
        [_allBtn setTitleColor:UIColorFromHex(kColorMain) forState:UIControlStateNormal];
        _allBtn.titleLabel.font = UIFontSize(11);
        _allBtn.layer.cornerRadius = 5;
        _allBtn.layer.masksToBounds = YES;
        _allBtn.layer.borderWidth = kOnePixel;
        _allBtn.layer.borderColor = UIColorFromHex(kColorMain).CGColor;
    }
    return _allBtn;
}

-(UIButton *)cuoBtn
{
    if (!_cuoBtn) {
        _cuoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cuoBtn setBackgroundColor:UIColorFromHex(kColorWhite) forState:UIControlStateNormal];
        [_cuoBtn setBackgroundColor:UIColorFromHex(kColorMain) forState:UIControlStateSelected];
        [_cuoBtn setTitle:@"只看错题" forState:UIControlStateNormal];
        [_cuoBtn setTitleColor:UIColorFromHex(kColorWhite) forState:UIControlStateSelected];
        [_cuoBtn setTitleColor:UIColorFromHex(kColorMain) forState:UIControlStateNormal];
        _cuoBtn.titleLabel.font = UIFontSize(11);
        _cuoBtn.layer.cornerRadius = 5;
        _cuoBtn.layer.masksToBounds = YES;
        _cuoBtn.layer.borderWidth = kOnePixel;
        _cuoBtn.layer.borderColor = UIColorFromHex(kColorMain).CGColor;
    }
    return _cuoBtn;
}

@end
